package com.home.admin.base;

import com.home.base.core.framework.msg.ServerResponse;
import com.home.common.annotation.CheckApiClient;
import com.home.common.constants.client.ClientConstant;
import com.home.common.framework.core.controller.BaseController;
import com.home.common.handler.exception.BizException;
import com.home.common.utils.string.StringUtils;
import com.home.webapp.base.entity.BaseUser;
import com.home.webapp.base.service.BaseUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;


/**
 * Description: 用户信息 controller
 * Created by 王大宸 on 2020-10-05 23:28:38
 * Created with IntelliJ IDEA.
 */
@Controller
@RequestMapping(value = "/base/user")
@CheckApiClient(title = "用户管理", clientId = ClientConstant.CLIENT_WEB)
public class BaseUserController extends BaseController<BaseUserService, BaseUser, String> {
    private static final Logger logger = LoggerFactory.getLogger(BaseUserController.class);

    private static final String PATH_PREFIX = "base/user";

    /***
     * 功能描述：跳转到信息管理界面
     *
     * @author 王大宸
     * @date 2020-10-05 23:28:38
     * @param
     * @return java.lang.String
     */
    @GetMapping(value = "/index")
    @RequiresPermissions("base:user:index")
    public String index() {
        return PATH_PREFIX + "/index";
    }

    /***
     * 功能描述：跳转到添加页面
     *
     * @author 王大宸
     * @date 2020-10-05 23:28:38
     * @param
     * @return java.lang.String
     */
    @GetMapping(value = "/add")
    @RequiresPermissions("base:user:add")
    public String add() {
        return PATH_PREFIX + "/add";
    }

    /***
     * 功能描述：跳转到修改页面
     *
     * @author 王大宸
     * @date 2020-10-05 23:28:38
     * @param id          需要修改数据的id
     * @param modelMap   modelMap
     * @return java.lang.String
     */
    @GetMapping(value = "/edit/{id}")
    @RequiresPermissions("base:user:edit")
    public String edit(@PathVariable("id") String id, ModelMap modelMap) throws Exception {
        BaseUser user = baseService.getById(id);
        String avatar = user.getAvatar();
        if (StringUtils.isEmpty(avatar)) {
            avatar = "/static/home/images/avatar.jpg";
        }
        modelMap.put("baseUser", user);
        modelMap.put("avatar", avatar);
        return PATH_PREFIX + "/edit";
    }

    /***
     * 功能描述：跳转到查看页面
     *
     * @author 王大宸
     * @date 2020-10-05 23:28:38
     * @param id          需要查看数据的id
     * @param modelMap    modelMap
     * @return java.lang.String
     */
    @GetMapping(value = "/see/{id}")
    @RequiresPermissions("base:user:see")
    public String see(@PathVariable("id") String id, ModelMap modelMap) throws Exception {
        BaseUser user = baseService.getById(id);
        modelMap.put("baseUser", user);
        modelMap.put("avatar", user.getAvatar());
        return PATH_PREFIX + "/see";
    }

    /****
     * 功能说明：更改用户状态
     *
     * @author 王大宸
     * @date 2020/10/6 23:19
     * @param id       用户id
     * @param status   用户状态修改
     * @return com.home.common.framework.msg.ServerResponse<com.home.webapp.base.entity.BaseUser>
     */
    @PutMapping(value = "/{id}/status/{status}")
    @ResponseBody
    public ServerResponse<BaseUser> userStatus(@PathVariable("id") String id, @PathVariable("status") String status) throws BizException {
        BaseUser baseUser = new BaseUser();
        baseUser.setId(id);
        baseUser.setStatus(status);
        baseService.updateInfo(baseUser);
        return ServerResponse.ok();
    }

    /***
     * 功能说明：跳转到用户权限页面
     *
     * @author 王大宸
     * @date 2020/10/8 15:23
     * @param userId     用户id
     * @param modelMap   modelMap
     * @return java.lang.String
     */
    @GetMapping(value = "/power/{userId}")
    public String power(@PathVariable("userId") String userId, ModelMap modelMap) {
        modelMap.put("userId", userId);
        return PATH_PREFIX + "/power";
    }

    /***
     * 功能说明：跳转到权限下发页面
     *
     * @author 王大宸
     * @date 2020/10/8 15:23
     * @param userId    用户id
     * @param modelMap  modelMap
     * @return java.lang.String
     */
    @GetMapping(value = "/issue/power/{userId}")
    public String issuePower(@PathVariable("userId") String userId, ModelMap modelMap) {
        modelMap.put("userId", userId);
        return PATH_PREFIX + "/issue_power";
    }

}